﻿using System;
using System.Collections;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.WebControls;

using System.Web.Script.Serialization;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using bridge.Views;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using AjaxControlToolkit;

namespace bridge
{
    
    public partial class Home : System.Web.UI.Page
    {
        public static int dynamicID = 0;
        BannerObject[] myBannerObject = new BannerObject[10];
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

               
                myBannerObject[0] = new BannerObject();
                myBannerObject[0].insertBannerObject("check");

                List<BannerObject> bo = getBannerObject();
                for (int i = 0; i < bo.Count;i++ )
                {
                    generateCollection(bo[i]);

                }
                //Session[information] = myBannerObject[i];
                generateCollection(myBannerObject[0]);
                
              /*  for (int i = 0; i < 10; i++)
                {
                    string information = "Nice Nepali Song_" + i;
                    string facebookLink = "https://www.youtube.com/watch?v=jJAGjgHOB8o";
                    string imageLink = "../images/picture.png";
                    string youtubeLink = "https://www.youtube.com/watch?v=jJAGjgHOB8o";
                    string youtubeChannel = "SongsNepal";
                    // href="https://twitter.com/share" data-via="Notepad_plus"
                    string tweetLink = "https://twitter.com/share";
                    string albumName = "Spring";
                    string artistName = "Suman Chhetri";
                    int publishedYear = 2010;
                    myBannerObject[i] = new BannerObject(information,albumName,artistName,publishedYear, facebookLink, tweetLink, youtubeLink, youtubeChannel,imageLink,5,4,3,2,1,10,i%5, i );
                    Session[information] = myBannerObject[i];
                    generateCollection(myBannerObject[i]);
                }*/
            }
        }

        private string GetConnectionString()
        {
            return ConfigurationManager.ConnectionStrings["zzCS321_19ConnectionString"].ConnectionString;
        }

        //*Update object Data*//
        public List<BannerObject> getBannerObject()
        {
           
            List<BannerObject>bannerObject=new List<BannerObject>();


            string sqlSong = "SELECT SongID, Title, PublishedDate, ArtistID, AlbumID "
                            + "FROM Song ";

            

            string sqlLinks = "SELECT FacebookLink, YoutubeLink, TweeterLink "
                            + "FROM Links "
                            + "WHERE SongID = @songID";
          
            

            using (SqlConnection con = new SqlConnection(GetConnectionString()))
            {
                using (SqlCommand cmdSong = new SqlCommand(sqlSong, con))
                {
                    con.Open();
                    SqlDataReader dr = cmdSong.ExecuteReader(CommandBehavior.CloseConnection);
                    while (dr.Read())
                    {
                        BannerObject b = new BannerObject();
                        b.Song.SongID = dr["SongID"].ToString();
                        b.Song.Title = dr["Title"].ToString();
                        b.Song.PublishedDate = dr["PublishedDate"].ToString();
                        b.Song.ArtistID = dr["ArtistID"].ToString();
                        b.Song.AlbumID = dr["AlbumID"].ToString();
                        b.Artist = getArtist(b.Song.ArtistID);
                        b.Rating = getRating(b.Song.SongID);
                        bannerObject.Add(b);
                    }
                    dr.Close();
                }
            }


            return bannerObject;
        }
        //**//
        public Artist getArtist(string artistID)
        {
            Artist a = new Artist();
           

            string sqlArtist = "SELECT FirstName, LastName, EMail, PhoneNo "
                            + "FROM Person "
                            + "WHERE PersonID = @PersonID";
            using (SqlConnection conArtist = new SqlConnection(GetConnectionString())){
           
                using (SqlCommand cmdArtist = new SqlCommand(sqlArtist, conArtist))
                {
                    try
                    {
                        cmdArtist.Parameters.AddWithValue("PersonID", artistID);
                        conArtist.Open();
                        SqlDataReader dr = cmdArtist.ExecuteReader(CommandBehavior.CloseConnection);
                        if (dr.Read())
                        {
                            a.ArtistID = artistID;
                            a.FirstName = dr["FirstName"].ToString();
                            a.LastName = dr["LastName"].ToString();
                            a.Email = dr["EMail"].ToString();
                            a.PhoneNo = dr["PhoneNo"].ToString();
                        }
                        dr.Close();

                    }
                    catch(Exception e)
                    {
                        lblError.Text += e.ToString();
                    }
                    }
                conArtist.Close();
            }



            return a;

        }

        public Ratings getRating( string songID)
        {
            Ratings r = new Ratings();


            string sqlRating = "SELECT FiveStars, FourStars, ThreeStars, TwoStars, OneStars "
                            + "FROM Rating "
                            + "WHERE SongID = @SongID";

           
            using (SqlConnection conRating = new SqlConnection(GetConnectionString()))
            {

                using (SqlCommand cmdRating = new SqlCommand(sqlRating, conRating))
                {
                    try
                    {
                        cmdRating.Parameters.AddWithValue("SongID", songID);
                        conRating.Open();
                        SqlDataReader dr = cmdRating.ExecuteReader(CommandBehavior.CloseConnection);
                        if (dr.Read())
                        {
                            r.SongID = songID;
                            r.FiveStars = Convert.ToInt32(dr["FiveStars"]);
                            r.FourStars = Convert.ToInt32(dr["FourStars"]);
                            r.ThreeStars = Convert.ToInt32(dr["ThreeStars"]);
                            r.TwoStars= Convert.ToInt32(dr["TwoStars"]);
                            r.OneStars = Convert.ToInt32(dr["OneStars"]);                            
                        }
                        dr.Close();

                    }
                    catch (Exception e)
                    {
                        lblError.Text += e.ToString();
                    }
                }
                conRating.Close();
            }



            return r;

        }

        public void generateCollection(BannerObject bannerObject)
        {

            PlaceHolder1.Controls.Add(AddPostRow(bannerObject));
        }

        private HtmlGenericControl AddPostRow(BannerObject bannerObject)
        {
            HtmlGenericControl NewControl = new HtmlGenericControl("div");
            NewControl.ID = "idPostRow_" + bannerObject.Song.SongID;
            NewControl.Attributes.Add("class", "divPostRow");
            NewControl.Controls.Add(AddPost(bannerObject));
            NewControl.Controls.Add(AddRating(bannerObject));
            NewControl.Controls.Add(AddSocialInfo(bannerObject));
           NewControl.Controls.Add(AddHiddenDetail(bannerObject));
            NewControl.Controls.Add(AddExpandDetail(bannerObject));
            return NewControl;
        }
        private HtmlGenericControl AddPost(BannerObject bannerObject)
        {
            HtmlGenericControl NewControl = new HtmlGenericControl("div");
            NewControl.ID = "idPost_" + bannerObject.Song.SongID;
            NewControl.Attributes.Add("class", "divPost");
            HtmlGenericControl img = new HtmlGenericControl("img");
            img.ID = "idImg_" + bannerObject.Song.SongID;
            img.Attributes.Add("class", "postImg");
         //   img.Attributes.Add("src", bannerObject.ImageLink);
            NewControl.Controls.Add(img);
            NewControl.Controls.Add(AddPostInfo(bannerObject ));
            return NewControl;
        }
        private HtmlGenericControl AddPostInfo(BannerObject bannerObject) {
            HtmlGenericControl NewControl = new HtmlGenericControl("div");
            NewControl.ID = "idPostInfo_" + bannerObject.Song.SongID;
            NewControl.Attributes.Add("class", "divPostInfo");
            LiteralControl title = new LiteralControl("<h1>" + bannerObject.Song.Title + "</h1>");
            NewControl.Controls.Add(title);
            LiteralControl album = new LiteralControl("<br/>Album: <a href = \"#\"> "+bannerObject.Album.Title +"</a>");
            NewControl.Controls.Add(album);
            LiteralControl year = new LiteralControl("<br/>Published Year: <a href = \"#\"> "+bannerObject.Song.PublishedDate+" </a>");
            NewControl.Controls.Add(year);
            LiteralControl artist = new LiteralControl("<br/>Artist: <a href = \"#\"> "+bannerObject.Artist.FirstName +" "+bannerObject.Artist.LastName+"</a>");
            NewControl.Controls.Add(artist);
           
            return NewControl;
        }
       private HtmlGenericControl AddRating(BannerObject bannerObject)
        {
            HtmlGenericControl NewControl = new HtmlGenericControl("div");
            NewControl.ID = "idRating_" + bannerObject.Song.SongID;
            NewControl.Attributes.Add("class", "divRating");
            LiteralControl literalControl = new LiteralControl("<h9>Avg Rating:" + bannerObject.Rating.AverageStars + "</h9>");
            AjaxControlToolkit.Rating rating = new AjaxControlToolkit.Rating();
            rating.ID = "ID_" + bannerObject.Song.SongID;
            rating.AutoPostBack = false;
            //rating.Changed = OnRatingChanged();
            rating.StarCssClass = "Star";
            rating.WaitingStarCssClass = "WaitingStar";
            rating.EmptyStarCssClass = "Star";
            rating.FilledStarCssClass = "FilledStar";
            rating.CurrentRating = Convert.ToInt32(bannerObject.Rating.AverageStars);
            NewControl.Controls.Add(rating);

            HtmlGenericControl divRatingLabel = new HtmlGenericControl("div");
            divRatingLabel.ID = "idRatingLabel_"+bannerObject.Song.SongID;
            divRatingLabel.Attributes.Add("class", "divRatingLabel");
            divRatingLabel.InnerText = "Total "+bannerObject.Rating.TotalStars+" votes";
            NewControl.Controls.Add(divRatingLabel);
            return NewControl;
        }
        private HtmlGenericControl AddSocialInfo(BannerObject bannerObject)
        {
            HtmlGenericControl NewControl = new HtmlGenericControl("div");
            NewControl.ID = "idSocialInfo_" + bannerObject.Song.SongID;
            NewControl.Attributes.Add("class", "divSocialInfo");
            NewControl.Controls.Add(AddYoutube(bannerObject));
            NewControl.Controls.Add(AddFacebook(bannerObject));
            NewControl.Controls.Add(AddTweeter(bannerObject));
            NewControl.Controls.Add(AddGoogle(bannerObject));
            return NewControl;
        }
        private HtmlGenericControl AddGoogle(BannerObject bannerObject)
        {
            HtmlGenericControl NewControl = new HtmlGenericControl("div");
            NewControl.ID = "idGoogle_" + bannerObject.Song.SongID;
            NewControl.Attributes.Add("class", "divGoogle");
            LiteralControl literalControl = new LiteralControl("<div class=\"g-plusone\"></div>");
            NewControl.Controls.Add(literalControl);
            return NewControl;
        }
        private HtmlGenericControl AddFacebook(BannerObject bannerObject)
        {
            HtmlGenericControl NewControl = new HtmlGenericControl("div");
            NewControl.ID = "idFacebook_" + bannerObject.Song.SongID;
            NewControl.Attributes.Add("class", "divFacebook");
            LiteralControl literalControl = new LiteralControl("<div id =" + bannerObject.Song.SongID + " class=\"fb-like\" data-href=" + bannerObject.Links.FacebookLink + " data-layout=\"button_count\" data-action=\"like\" data-show-faces=\"false\" data-share=\"false\"></div>");
            NewControl.Controls.Add(literalControl);
            return NewControl;
        }
        private HtmlGenericControl AddTweeter(BannerObject bannerObject)
        {
            HtmlGenericControl NewControl = new HtmlGenericControl("div");
            NewControl.ID = "idTwetter_" + bannerObject.Song.SongID;
            NewControl.Attributes.Add("class", "divTwetter");
            LiteralControl literalControl = new LiteralControl("<a id=" + bannerObject.Song.SongID + " class=\"twitter-share-button\" href=" + bannerObject.Links.TweeterLink + "data-via=\"Notepad_plus\">Tweet</a>");
            NewControl.Controls.Add(literalControl);
            return NewControl;
        }
        private HtmlGenericControl AddYoutube(BannerObject bannerObject)
        {
            HtmlGenericControl NewControl = new HtmlGenericControl("div");
            NewControl.ID = "idYoutube_" + bannerObject.Song.SongID;
            NewControl.Attributes.Add("class", "divYoutube");
            LiteralControl literalControl = new LiteralControl("<div class=\"g-ytsubscribe\" data-channel="+bannerObject.Links.YoutubeLink+" data-layout=\"default\" data-count=\"default\"></div>");
            NewControl.Controls.Add(literalControl);
            return NewControl;
        }
        private HtmlGenericControl AddExpandDetail(BannerObject bannerObject)
        {
            HtmlGenericControl NewControl = new HtmlGenericControl("div");
            NewControl.ID = "idExpandOption_" + bannerObject.Song.SongID;
            NewControl.Attributes.Add("class", "divExpandDetail");
            NewControl.InnerText = "⇊⇓⇊";
            return NewControl;
        }
        private HtmlGenericControl AddHiddenDetail(BannerObject bannerObject)
        {
            HtmlGenericControl NewControl = new HtmlGenericControl("div");
            NewControl.ID = "idHiddenDetail_" + bannerObject.Song.SongID;
            NewControl.Attributes.Add("class", "divHiddenDetail");
            NewControl.Controls.Add(AddBarChartRating(bannerObject));
           
            return NewControl;
        }

        private HtmlGenericControl AddBarChartRating(BannerObject bannerObject)
        {
            HtmlGenericControl NewControl = new HtmlGenericControl("div");
            NewControl.ID = "idBarChartRating_" + bannerObject.Song.SongID;
            NewControl.Attributes.Add("class", "divBarChartRating");

            Decimal[] data = { bannerObject.Rating.OneStars,bannerObject.Rating.TwoStars,bannerObject.Rating.ThreeStars,bannerObject.Rating.FourStars,bannerObject.Rating.FiveStars};

           
            AjaxControlToolkit.BarChart barChart = new AjaxControlToolkit.BarChart();
            barChart.ChartHeight = "150";
            barChart.ChartWidth = "220";
            barChart.CategoriesAxis = "1★  ,2★  ,3★  ,4★  ,5★  ";
            barChart.ChartType = AjaxControlToolkit.ChartType.Bar;
            barChart.CategoryAxisLineColor = "none";
            barChart.BaseLineColor = "blue";
            barChart.ValueAxisLineColor = "none";
          
            barChart.Series.Add(new AjaxControlToolkit.BarChartSeries{  BarColor = "gray", Data=data,Name="Votes" });
           
            NewControl.Controls.Add(barChart);

            return NewControl;
        }

      
        protected void OnRatingChanged(object sender, RatingEventArgs e)
        {
            /* string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
             using (SqlConnection con = new SqlConnection(constr))
             {
                 using (SqlCommand cmd = new SqlCommand("INSERT INTO UserRatings VALUES(@Rating)"))
                 {
                     using (SqlDataAdapter sda = new SqlDataAdapter())
                     {
                         cmd.CommandType = CommandType.Text;
                         cmd.Parameters.AddWithValue("@Rating", e.Value);
                         cmd.Connection = con;
                         con.Open();
                         cmd.ExecuteNonQuery();
                         con.Close();
                     }
                 }
             }
             Response.Redirect(Request.Url.AbsoluteUri);
             * */
        }

    /*    private void CreateRatingDisplay()
        {
            AjaxControlToolkit.Rating rating = new AjaxControlToolkit.Rating();
            rating.ID = "ratingID";
            rating.CurrentRating = 2;
            rating.StarCssClass = "Star";
            rating.EmptyStarCssClass = "Star";

            rating.FilledStarCssClass = "FilledStar";
            rating.WaitingStarCssClass = "WaitingStar";
           
            rating.AutoPostBack = false;
          //  lblItems.Controls.a
            
           // rating.Changed = OnRatingChanged();
        }*/

    }
}